package com.team3.ecommerce.service;

import com.team3.ecommerce.domain.Commodity;
import com.team3.ecommerce.domain.CommodityExample;
import com.team3.ecommerce.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommodityServiceImp implements CommodityService{
    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public Commodity findCommodityById(String id) {
        return commodityMapper.selectByPrimaryKey(id);
    }



    @Override
    public List<Commodity> searchCommodity(String searchString) {
        searchString = "%"+searchString+"%";
        CommodityExample example = new CommodityExample();
        example.or().andCommNameLike(searchString);
        example.or().andCommTypeLike(searchString);
        example.or().andCommDescriptionLike(searchString);
        example.setDistinct(true);
        return commodityMapper.selectByExample(example);
    }

    @Override
    public List<Commodity> searchCommodityTypeLimited(String searchString,String commType) {
        searchString = "%"+searchString+"%";
        CommodityExample example = new CommodityExample();
        example.or().andCommNameLike(searchString).andCommTypeEqualTo(commType);
        example.or().andCommTypeLike(searchString).andCommTypeEqualTo(commType);
        example.or().andCommDescriptionLike(searchString).andCommTypeEqualTo(commType);
        example.setDistinct(true);
        return commodityMapper.selectByExample(example);
    }

    @Override
    public List<Commodity> commodityFilter(String searchString,String commType, String commStock, int priceLowerBound, int priceUpperBound) {
        searchString = "%"+searchString+"%";
        CommodityExample example = new CommodityExample();
        example.or().andCommNameLike(searchString);
        example.or().andCommTypeLike(searchString);
        example.or().andCommDescriptionLike(searchString);
        if (commType != "所有"){
            example.or().andCommDescriptionLike(searchString).andCommTypeEqualTo(commType);
        }
        if (commStock == "有存货"){
            example.or().andCommStockGreaterThan(0);
        }
        else {
            example.or().andCommStockNotEqualTo(0);
        }
        example.or().andCommPriceBetween((float)priceLowerBound,(float)priceUpperBound);
        example.setDistinct(true);
        return commodityMapper.selectByExample(example);
    }
}
